package com.ziyun.erp.web.modules.facilitator.controller;

import com.ziyun.erp.modules.facilitator.entity.FacilitatorEntity;
import com.ziyun.erp.modules.facilitator.service.FacilitatorService;
import com.ziyun.erp.modules.freightages.service.SdbEctoolsRegionsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;



/**
 * 服务商管理
 *
 * @author jiangli
 * @email 
 * @date 2018-12-29 16:20:17
 */
@RestController
@RequestMapping("facilitator/facilitator")
public class FacilitatorController {
    @Autowired
    private FacilitatorService facilitatorService;
    @Autowired
    private SdbEctoolsRegionsService sdbEctoolsRegionsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("facilitator:facilitator:list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = facilitatorService.queryPage(params);
        // 查询省市区级联数据
        List<String> regions = sdbEctoolsRegionsService.queryRegionsSascade();

        return R.ok().put("page", page).put("regions", JSONArray.fromObject(regions).toString());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{teamId}")
    @RequiresPermissions("facilitator:facilitator:info")
    public R info(@PathVariable("teamId") Integer teamId){
        FacilitatorEntity facilitator = facilitatorService.getFacilitatorEntity(teamId);

        return R.ok().put("facilitator", facilitator);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("facilitator:facilitator:save")
    public R save(@RequestBody FacilitatorEntity facilitator){
       return facilitatorService.saveFacilitatorEntity(facilitator);

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("facilitator:facilitator:update")
    public R update(@RequestBody FacilitatorEntity facilitator){
        return facilitatorService.updateFacilitatorEntity(facilitator);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("facilitator:facilitator:delete")
    public R delete(@RequestBody List<FacilitatorEntity> facilitatorEntityList){
        facilitatorService.deleteFacilitatorEntity(facilitatorEntityList);

        return R.ok();
    }

    //查询省市区下的商家
    @RequestMapping("/getShops/{area}")
    public R getShops(@PathVariable String area) {
        List<Map<String, Object>> shops = facilitatorService.getShops(area);
        return R.ok().put("shops",shops);
    }

    //审核页面省市区及商户
    @RequestMapping("/chenckShop/{userId}")
    public R chenckShop(@PathVariable Integer userId) {
       return facilitatorService.chenckShop(userId);
    }

    //审核通过/拒绝
    @RequestMapping("/refuse/{id}/{flag}")
    public R refuse(@PathVariable Integer id,@PathVariable Integer flag) {
      return facilitatorService.refuse(id,flag);
    }

    //搜索
    @RequestMapping("/searchQ")
    public R searchQ(@RequestBody Map<String, Object> params) {
       return facilitatorService.searchQ(params);
    }

    //服务商分析
    @RequestMapping("/facilitatorData")
    public R facilitatorData(@RequestBody Map<String, Object> params) {
       return facilitatorService.facilitatorData(params);
    }

    //根据team_id查询代理商
    @RequestMapping("/userDistributionList/{teamId}")
    public R userDistributionList(@PathVariable Integer teamId) {
        return facilitatorService.userDistributionList(teamId);
    }

    //服务商统计
    @RequestMapping("/facilitatorStatisticVo")
    public R facilitatorStatisticVo(@RequestBody Map<String, Object> params) {
        return facilitatorService.facilitatorStatisticVo(params);
    //    return R.ok().put("page",page);
    }

}
